package com.homework.action;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.homework.bean.CourseBean;
import com.homework.bean.PageInfoBean;
import com.homework.bean.UserBean;
import com.homework.bean.UserListBean;
import com.homework.service.UserService;

public class UserQueryAction {

	private static UserService userService = null;
	private int courseId;
	private int intPage;
	private String realname;

	public UserQueryAction() {
		userService = new UserService();
	}

	public int getCourseId() {
		return courseId;
	}

	public void setCourseId(int courseId) {
		this.courseId = courseId;
	}

	public int getIntPage() {
		return intPage;
	}

	public void setIntPage(int intPage) {
		this.intPage = intPage;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	// 学生查询
	public String studentQuery() throws SQLException {
		HttpServletRequest request = ServletActionContext.getRequest();

		// courseBean
		CourseBean courseBean = new CourseBean();
		courseBean.setCourseId(String.valueOf(this.getCourseId()));
		// pageInfoBean
		PageInfoBean pageInfoBean = new PageInfoBean();
		pageInfoBean.setIntPage(this.getIntPage());
		// courseId
		int courseIdTemp = this.getCourseId();
		// userBean
		UserBean userBean = new UserBean();
		userBean.setRealname(this.getRealname()); 

		// 处理和设置pageInfoBean,StudentHomeworkListBean
		UserListBean userListBean = new UserListBean();
		userListBean = userService.studentQuery(courseBean, pageInfoBean, userBean);
		request.setAttribute("userListBean", userListBean);
		request.setAttribute("pageInfoBean", pageInfoBean);
		request.setAttribute("courseId", courseIdTemp);
		
		return "Success";
	}

	// 用户查询（管理员）
	public String userQuery() throws SQLException {
		HttpServletRequest request = ServletActionContext.getRequest();

		// pageInfoBean
		PageInfoBean pageInfoBean = new PageInfoBean();
		pageInfoBean.setIntPage(this.getIntPage());
		// userBean
		UserBean userBean = new UserBean();
		userBean.setRealname(this.getRealname());

		// 处理和设置pageInfoBean
		UserListBean userListBean = new UserListBean();
		userListBean = userService.userQuery(pageInfoBean,userBean);
		request.setAttribute("userListBean", userListBean);
		request.setAttribute("pageInfoBean", pageInfoBean);

		return "Success";
	}
}
